#include <windows.h>

DWORD WINAPI StartFun(LPVOID);
VOID EndFun()
{

}

int wmain()
{
	//Write ShellCode
	size_t dwSize = (LPBYTE)EndFun - (LPBYTE)StartFun;
	TCHAR szPath[MAX_PATH] = {0};
	GetModuleFileName(nullptr, szPath, MAX_PATH);
	for (int i = MAX_PATH - 1; i > 0; i--) {
		if ('\\' == szPath[i]) {
			szPath[i + 1] = 'C';
			szPath[i + 2] = 'o';
			szPath[i + 3] = 'd';
			szPath[i + 4] = 'e';
			szPath[i + 5] = '.';
			szPath[i + 6] = 'h';
			szPath[i + 7] = '\0';
			break;
		}
	}
	HANDLE hFile = CreateFile(szPath, GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL, nullptr);
	if (INVALID_HANDLE_VALUE == hFile) {
		StartFun(0);
		return 1;
	}
	DWORD dwWrited(0);
	BYTE Tmp[6];
	Tmp[0] = '0';
	Tmp[1] = 'x';
	Tmp[4] = ',';
	Tmp[5] = ' ';
	WriteFile(hFile, "BYTE g_ShellCode [] = {", 23, &dwWrited, nullptr);

	LPBYTE lpBuffer = (LPBYTE)StartFun;
	for (DWORD i = 0; i < dwSize; i++) {
		Tmp[2] = Tmp[3] = '0';
		BYTE t = lpBuffer[i];
		int j = 3;
		while (t){
			Tmp[j] = (t % 16);
			Tmp[j] += (Tmp[j] >= 10 ? 'A'-10 : '0');
			t = t / 16;
			j--;
		}

		WriteFile(hFile, &Tmp, sizeof(Tmp), &dwWrited, nullptr);
	}
	WriteFile(hFile, "};", 2, &dwWrited, nullptr);
	CloseHandle(hFile);

	return 0;
}
